package cn.tedu.egnotice1.controller;

import cn.tedu.egnotice1.base.response.JsonResult;
import cn.tedu.egnotice1.mapper.NoticeMapper;
import cn.tedu.egnotice1.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice1.pojo.dto.NoticeAddQuery;
import cn.tedu.egnotice1.pojo.entity.Notice;
import cn.tedu.egnotice1.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egnotice1.pojo.vo.NoticeListVO;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {


    @Autowired
    NoticeMapper noticeMapper;

    @PostMapping("add")


    public JsonResult add(NoticeAddParam noticeAddParam) {
        log.debug("noticeAddParam="+noticeAddParam);

        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        log.debug("notice = " + notice);
        notice.setUserId(100L);
        notice.setCreateTime(LocalDateTime.now());
        notice.setUpdateTime(LocalDateTime.now());
        log.debug("notice2 = " + notice);
        noticeMapper.insert(notice);
        return JsonResult.ok();
    }

    @GetMapping("list")


    public JsonResult list(NoticeAddQuery noticeAddQuery){
        log.debug("noticeAddQuery="+noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOS =noticeMapper.list(title,type);

        return JsonResult.ok(noticeListVOS);
    }

    @GetMapping("detail")
    @ResponseBody

    public JsonResult detail(Long id){
        log.debug("id = " + id);

        NoticeDetailInfoVO detailInfoVO = noticeMapper.detail(id);

        return JsonResult.ok(detailInfoVO);
    }

}
